/* eslint-disable no-use-before-define */
import type { Response } from '@/utils/axios/axios';

/**
 * 上传文件
 */
export function uploadAxios(data: { path: string; formData?: Record<string, unknown>; url?: string }): Promise<Response<UploadResponse>> {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: data.url || `${import.meta.env.VITE_BASE_UPLOAD_URL}/upload/uploadFile`,
      filePath: data.path,
      name: 'file',
      formData: data.formData || {},
      success: (res) => {
        if (res && res.statusCode === 200 && res.data) {
          const _data = JSON.parse(res.data) as Response<UploadResponse>;
          resolve(_data);
        } else {
          reject(JSON.parse(res.data) as Response);
        }
      },
      fail(error) {
        reject(error);
      },
    });
  });
}

export type UploadResponse = {
  /** 文件MD5 */
  fileHash?: string;
  /** 图片或视频URL */
  url: string;
  /** 地一帧图片 */
  frameUrl?: string;
  /** 图片或视频宽度 */
  width?: 1554;
  /** 图片或视频高度 */
  height?: 2049;
  /** 图片或视频大小 */
  size?: 59950;
};
